package ywh.player.media
{
	import flash.display.Stage;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.NetStatusEvent;
	import flash.events.StageVideoAvailabilityEvent;
	import flash.geom.Point;
	import flash.media.SoundTransform;
	import flash.media.StageVideo;
	import flash.media.StageVideoAvailability;
	import flash.net.NetConnection;
	import flash.net.NetStream;
	import flash.text.TextField;
	import ywh.event.PlayerEvent;
	import ywh.event.StageVideoMouseEvent;
	import ywh.list.CourseItem;
	import ywh.manager.Core;
	import ywh.manager.Tag;
	import ywh.player.AbstractPlayer;
	import ywh.player.av.AudioManager;
	import ywh.player.av.IAudioHandle;
	import ywh.player.PlayerState;
	import ywh.utils.TimeFormat;
	
	/**
	 * 屏幕视频
	 * @author ywh
	 */
	public class StageVideoPlayer extends AbstractPlayer implements IAudioHandle
	{
		private var _stageVideo:StageVideo;
		private var _stage:Stage;
		private var _netConnection:NetConnection;
		private var _netStream:NetStream;
		private var _url:String;
		private var isIOS:Boolean;
		private var _playerState:String;
		private var _playerDuration:Number;
		private var canSeek:Boolean;
		private var debuger:TextField;
		private var _netStreamState:String;
		
		override protected function get createPlayerDirectly():Boolean
		{
			return false;
		}
		
		public function StageVideoPlayer()
		{
			super();
		
		}
		
		override protected function initDisplayer():void
		{
			_stage = _layoutSprite.stageRef;
			_stage.addEventListener(StageVideoAvailabilityEvent.STAGE_VIDEO_AVAILABILITY, stage_stageVideoAvailability);
			_stage.addEventListener(MouseEvent.MOUSE_DOWN, stage_mouseDown);
			/**debuger**/
			debuger = new TextField();
			debuger.width = 800;
			debuger.height = 30;
			_stage.addChild(debuger)
		}
		
		override public function preSyncHandle():void
		{
			super.preSyncHandle();
			debuger.text = "当前视频：" + _playItem.getPropString(Tag._URL) + ",当前时间：" + TimeFormat.ss2hhmmss(_pTime) + "，当前状态为：" + _netStreamState;
		}
		
		private function stage_stageVideoAvailability(e:StageVideoAvailabilityEvent):void
		{
			if (e.availability == StageVideoAvailability.AVAILABLE)
			{
				_stage.removeEventListener(StageVideoAvailabilityEvent.STAGE_VIDEO_AVAILABILITY, stage_stageVideoAvailability);
				_netConnection = new NetConnection();
				_netConnection.connect(null);
				_netStream = new NetStream(_netConnection);
				_netStream.addEventListener(NetStatusEvent.NET_STATUS, netStream_netStatus);
				
				_stageVideo = _stage.stageVideos[0];
				_stageVideo.attachNetStream(_netStream);
				_layoutSprite.bandVideo = _stageVideo;
				dispatchEvent(PlayerEvent.PLAYER_INIT);
			}
		}
		
		private function stage_mouseDown(e:MouseEvent):void
		{
			if (_stageVideo && _stageVideo.viewPort.containsPoint(new Point(e.stageX, e.stageY)))
			{
				Core.dispatchEvent(new StageVideoMouseEvent(StageVideoMouseEvent.MOUSE_DOWN, e));
			}
		}
		
		private function netStream_netStatus(e:NetStatusEvent):void
		{
			_netStreamState = e.info.code;
			if (e.info.level == "error")
			{
				_playerState = PlayerState.PLAYBACK_ERROR;
			}
			else
			{
				switch (e.info.code)
				{
					case "NetConnection.Connect.Success": 
					case "NetConnection.Connect.Closed": 
						break;
					case "NetStream.Buffer.Empty":
					case "NetStream.Seek.Notify":
						_playerState = PlayerState.LOADING;
						break;					
					case "NetStream.Buffer.Full":
						canSeek = true;
					case "NetStream.Buffer.Flush": 
					case "NetStream.Play.Start":
					case "NetStream.Seek.Complete":										 
					case "NetStream.Unpause.Notify": 												
						_playerState = PlayerState.PLAYING;
						break;
					case "NetStream.Play.Stop": 
						_playerState = PlayerState.ENDED;
						break;
					case "NetStream.Pause.Notify": 
						_playerState = PlayerState.PAUSED;
					default: 
						break;
				}
			}
		}
		
		override public function set playItem(value:CourseItem):void
		{
			super.playItem = value;
			var isAbsolutePath:Boolean = _playItem.getPropBool(Tag._IS_ABSOLUTE_PATH);
			var urlString:String = _playItem.getPropString(Tag._PARH) + _playItem.getPropString(Tag._URL);
			if (!isAbsolutePath)
			{
				urlString = Core.getResolvePath(urlString);
			}
			_netStream.client = {};
			_netStream.client.onMetaData = function(info:Object)
			{
				_playerDuration = info.duration;
			};
			_netStream.play(urlString);
		}
		
		override protected function canSeekTo(value:Number):Boolean
		{
			if (canSeek)
			{
				return super.canSeekTo(value);
			}
			return false;
		}
		
		override protected function get _pState():String
		{
			return _playerState;
		}
		
		override protected function _pPause():void
		{
			_netStream.pause();
		}
		
		override protected function _pPlay():void
		{
			_netStream.resume();
		}
		
		override protected function get _pTime():Number
		{
			return _netStream.time;
		}
		
		public function get stageVideo():StageVideo
		{
			return _stageVideo;
		}
		
		override protected function _pSeekTo(value:Number):void
		{
			_netStream.seek(value);
		}
		
		override protected function get _pDuration():Number
		{
			return _playerDuration;
		}
		
		override protected function _pDestroy():void
		{
			canSeek = false;
			_playerDuration = NaN;
			_netStream.close();
		}
		
		/* INTERFACE ywh.player.av.IAudioHandle */
		
		public function set volume(value:Number):void
		{
			var transform:SoundTransform = _netStream.soundTransform;
			transform.volume = value;
			_netStream.soundTransform = transform;
		}
		
		public function get volume():Number
		{
			return _netStream.soundTransform.volume;
		}
	}

}